//
//  DateUtility.m
//  EVETracker
//
//  Created by Scott Guyer on 7/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "DateUtility.h"


@implementation DateUtility

+ (NSDate*) convertUTCToLocal:(NSDate*)utcDate 
{
	NSCalendar* lcal = [NSCalendar currentCalendar];
	
	NSTimeZone* zulu = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
	NSCalendar* zcal = [NSCalendar currentCalendar];
	[zcal setTimeZone:zulu];
	
	NSDateComponents* comp = [lcal components:0x03ff fromDate:utcDate];
	NSDate* local = [zcal dateFromComponents:comp];
	
	return local;
}

+ (NSDate*) convertLocalToUTC:(NSDate*)localDate 
{
	NSCalendar* lcal = [NSCalendar currentCalendar];
	
	NSTimeZone* zulu = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
	NSCalendar* zcal = [NSCalendar currentCalendar];
	[zcal setTimeZone:zulu];
	
	NSDateComponents* comp = [zcal components:0x03ff fromDate:localDate];
	NSDate* utc = [lcal dateFromComponents:comp];
	
	return utc;	
}

+ (NSString*) secondsToString:(int)seconds 
{	
	int days = (int) floor( seconds / 86400 );	
	seconds -= (days * 86400);
	
	int hours = (int) floor( seconds / 3600 );
	seconds -= (hours * 3600);
	
	int minutes = (int) floor( seconds / 60 );	
	seconds -= (minutes * 60);
	
	NSMutableString* result = [[NSMutableString alloc] initWithCapacity:64];
	
	BOOL first = YES;	
	if ( days > 0 )
	{
		first = NO;
		[result appendFormat:@"%s%dd", (first ? "" : " "), days];
	}
	
	if ( hours > 0 )
	{
		first = NO;
		[result appendFormat:@"%s%dh", (first ? "" : " "), hours];		
	}

	if ( minutes > 0 )
	{
		first = NO;
		[result appendFormat:@"%s%dm", (first ? "" : " "), minutes];		
	}

	if ( seconds > 0 )
	{
		first = NO;
		[result appendFormat:@"%s%ds", (first ? "" : " "), seconds];		
	}
	
	
	return result;
}

@end
